//
//  FinishedOrderHeader.m
//  RRBI
//
//  Created by kyjun on 16/5/4.
//
//

#import "FinishedOrderHeader.h"

@interface FinishedOrderHeader ()
@property(nonatomic,strong) UIView* statusView;
@property(nonatomic,strong) UILabel* labelNum;
@property(nonatomic,strong) NSLayoutConstraint* statusConstraint;
@property(nonatomic,strong) UILabel* labelStatus;
@property(nonatomic,strong) UIImageView* iconArrow;
@property(nonatomic,strong) UILabel* labelCreateDate;
@property(nonatomic,strong) UILabel* labelLine;
@property(nonatomic,strong) UILabel* labelPayType;
@property(nonatomic,strong) UILabel* labelUserName;
@property(nonatomic,strong) UIButton *zhqsBtn;// 召唤骑士

@property(nonatomic,strong) MOrder* entity;
@end

@implementation FinishedOrderHeader

-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if(self){
        self.contentView.backgroundColor = [UIColor colorWithRed:230/255.f green:230/255.f blue:230/255.f alpha:1.0];
        [self layoutUI];
        [self layoutConstraints];
        UITapGestureRecognizer *tapgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureTouch:)];
        [self addGestureRecognizer:tapgr];
    }
    return self;
}

#pragma mark =====================================================  user interface layout
-(void)layoutUI{
    [self addSubview:self.statusView];
    [self.statusView addSubview:self.labelNum];
    [self.statusView addSubview:self.labelStatus];
    [self.statusView addSubview:self.iconArrow];
    [self.statusView addSubview:self.labelCreateDate];
    [self.statusView addSubview:self.labelLine];
    [self.statusView addSubview:self.labelUserName];
    [self.statusView addSubview:self.labelPayType];
    [self.statusView addSubview:self.zhqsBtn];
}

-(void)layoutConstraints{
    self.statusView.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelNum.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelStatus.translatesAutoresizingMaskIntoConstraints = NO;
    self.iconArrow.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelCreateDate.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelLine.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelPayType.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelUserName.translatesAutoresizingMaskIntoConstraints = NO;
    self.zhqsBtn.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.statusView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:110.f]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.statusView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.statusView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.f]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.statusView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    [self.labelNum addConstraint:[NSLayoutConstraint constraintWithItem:self.labelNum attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:45.f]];
    [self.labelNum addConstraint:[NSLayoutConstraint constraintWithItem:self.labelNum attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:45.f]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelNum attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.statusView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelNum attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.statusView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    [self.labelStatus addConstraint:[NSLayoutConstraint constraintWithItem:self.labelStatus attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:25.f]];
    self.statusConstraint =[NSLayoutConstraint constraintWithItem:self.labelStatus attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/5];
    [self.labelStatus addConstraint:self.statusConstraint];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelStatus attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.statusView attribute:NSLayoutAttributeTop multiplier:1.0 constant:5.f]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelStatus attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.labelNum attribute:NSLayoutAttributeRight multiplier:1.0 constant:10.f]];
    
    [self.iconArrow addConstraint:[NSLayoutConstraint constraintWithItem:self.iconArrow attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:10.f]];
    [self.iconArrow addConstraint:[NSLayoutConstraint constraintWithItem:self.iconArrow attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:10.f]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.iconArrow attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.labelStatus attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.iconArrow attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.labelStatus attribute:NSLayoutAttributeRight multiplier:1.0 constant:5.f]];
    
    [self.labelCreateDate addConstraint:[NSLayoutConstraint constraintWithItem:self.labelCreateDate attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:25.f]];
    [self.labelCreateDate addConstraint:[NSLayoutConstraint constraintWithItem:self.labelCreateDate attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH*2/5]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelCreateDate attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.statusView attribute:NSLayoutAttributeTop multiplier:1.0 constant:5.f]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelCreateDate attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.statusView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
    
    [self.labelLine addConstraint:[NSLayoutConstraint constraintWithItem:self.labelLine attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:1.f]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelLine attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.labelNum attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelLine attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.labelNum attribute:NSLayoutAttributeRight multiplier:1.0 constant:5.f]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelLine attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.statusView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
    
    [self.labelUserName addConstraint:[NSLayoutConstraint constraintWithItem:self.labelUserName attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:25.f]];
    [self.labelUserName addConstraint:[NSLayoutConstraint constraintWithItem:self.labelUserName attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH/2]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelUserName attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelLine attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5.f]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelUserName attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.labelNum attribute:NSLayoutAttributeRight multiplier:1.0 constant:10.f]];
    
    
    [self.labelPayType addConstraint:[NSLayoutConstraint constraintWithItem:self.labelPayType attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:25.f]];
    [self.labelPayType addConstraint:[NSLayoutConstraint constraintWithItem:self.labelPayType attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:80]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelPayType attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelLine attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5.f]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelPayType attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.statusView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
    
    [self.zhqsBtn addConstraint:[NSLayoutConstraint constraintWithItem:self.zhqsBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:25.f]];
    [self.zhqsBtn addConstraint:[NSLayoutConstraint constraintWithItem:self.zhqsBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:80]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.zhqsBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelPayType attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5.f]];
    [self.statusView addConstraint:[NSLayoutConstraint constraintWithItem:self.zhqsBtn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.statusView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
}

-(void)loadData:(MOrder *)entity index:(NSInteger)index{
    if(entity){
        _entity = entity;
        UIColor* color;
        NSString* statusName;
        NSString* imageName;
        switch (entity.status) {
            case OrderStatusNewsOrder:
            {
                self.labelStatus.text = @"新订单";
                color = [UIColor colorWithRed:255/255.f green:162/255.f blue:0/255.f alpha:1.0];
                imageName = @"icon-arrow-finished";
            }
                break;
            case OrderStatusQiangOrder:
            {
                self.labelStatus.text = @"待抢单";
                color = [UIColor colorWithRed:41/255.f green:189/255.f blue:221/255.f alpha:1.0];
            }
                break;
            case OrderStatusGetingGoods:
            {
                self.labelStatus.text = @"取货中";
                color = [UIColor colorWithRed:41/255.f green:189/255.f blue:221/255.f alpha:1.0];
                imageName = @"icon-arrow-finished";
            }
                break;
            case OrderStatusOnTheWay:
            {
                self.labelStatus.text = @"配送中";
                color = [UIColor colorWithRed:135/255.f green:135/255.f blue:135/255.f alpha:1.0];
                imageName = @"icon-arrow-cancel";
            }
                break;
                
            case OrderStatusAlredayGetGoods:
            {
                self.labelStatus.text = @"已送达";
                color = [UIColor colorWithRed:135/255.f green:135/255.f blue:135/255.f alpha:1.0];
                imageName = @"icon-arrow-cancel";
            }
                break;
                
            case OrderStatusUsersMakeSure:
            {
                self.labelStatus.text = @"已确认";
                color = [UIColor redColor];
                imageName = @"icon-arrow-ing";
            }
                break;
            case OrderStatusAlredayPingja:
            {
                self.labelStatus.text = @"已评价";
                color = [UIColor redColor];
                imageName = @"icon-arrow-ing";
            }
                break;
            case OrderStatusChancleOrder:
            {
                self.labelStatus.text = @"已取消";
                color = [UIColor redColor];
                imageName = @"icon-arrow-ing";
            }
                break;
            case OrderStatusIngMakeSure:
            {
                self.labelStatus.text = @"待确认";
                color = [UIColor redColor];
                imageName = @"icon-arrow-ing";
            }
                break;
            case OrderStatusAlredayJieDan:
            {
                self.labelStatus.text = @"已接单";
                color = [UIColor redColor];
                imageName = @"icon-arrow-ing";
            }
                break;
            default:
                break;
        }
        self.labelNum.textColor = color;
        self.labelNum.layer.borderColor = color.CGColor;
        self.labelStatus.textColor = color;
        self.labelCreateDate.textColor =color;
        self.labelLine.backgroundColor = color;
        self.labelUserName.textColor = color;
        self.labelPayType.textColor = color;
        self.labelPayType.layer.borderColor = color.CGColor;
        [self.zhqsBtn setTitleColor:color forState:UIControlStateNormal];
        if(entity.status == OrderStatusPayed){
            self.iconArrow.hidden = YES;
        }else{
            self.iconArrow.hidden = NO;
            [self.iconArrow setImage:[UIImage imageNamed:imageName]];
        }
        CGFloat width = [WMHelper calculateTextWidth:statusName font:[UIFont systemFontOfSize:14.f] height:25.f];
        if(self.statusConstraint){
            [self.labelStatus removeConstraint:self.statusConstraint];
            self.statusConstraint =[NSLayoutConstraint constraintWithItem:self.labelStatus attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:width];
            self.statusConstraint.priority = 501;
            [self.labelStatus addConstraint:self.statusConstraint];
        }
//        self.labelStatus.text = statusName;
        if(entity.isExpansion){
            [UIView animateWithDuration:0.5 animations:^{
                self.iconArrow.transform = CGAffineTransformMakeRotation(M_PI/2);
            } completion:^(BOOL finished) {
                
            }];
            
        }else {
            [UIView animateWithDuration:0.5 animations:^{
                self.iconArrow.transform = CGAffineTransformMakeRotation(0);
            } completion:^(BOOL finished) {
                
            }];
            
        }
        self.labelNum.text = [NSString stringWithFormat:@"%02ld",index];
        self.labelCreateDate.text = [NSString stringWithFormat:@"下单时间:%@",entity.createDate];
        self.labelPayType.text = entity.payType;
        self.labelUserName.text = [NSString stringWithFormat:@"%@ / %@",entity.userName,entity.phone];
        
    }
}


#pragma mark =====================================================  SEL
-(IBAction)gestureTouch:(UIGestureRecognizer*)sender{
    if(self.delegate && [self.delegate respondsToSelector:@selector(orderHeader:)]){
        [self.delegate orderHeader:self.entity];
    }
}

#pragma mark =====================================================  property package
-(UIView *)statusView{
    if(!_statusView){
        _statusView = [[UIView alloc]init];
        _statusView.backgroundColor = [UIColor whiteColor];
    }
    return _statusView;
}

-(UILabel *)labelNum {
    if(!_labelNum){
        _labelNum = [[UILabel alloc]init];
        _labelNum.font = [UIFont systemFontOfSize:24.f];
        _labelNum.textAlignment = NSTextAlignmentCenter;
        _labelNum.layer.masksToBounds = YES;
        _labelNum.layer.cornerRadius = 5.f;
        _labelNum.layer.borderWidth = 1.f;
    }
    return _labelNum;
}

-(UILabel *)labelStatus{
    if(!_labelStatus){
        _labelStatus = [[UILabel alloc]init];
        _labelStatus.textAlignment = NSTextAlignmentLeft;
        _labelStatus.font = [UIFont systemFontOfSize:14.f];
    }
    return _labelStatus;
}

-(UIImageView *)iconArrow{
    if(!_iconArrow){
        _iconArrow = [[UIImageView alloc]init];
        _iconArrow.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _iconArrow;
}

-(UILabel *)labelCreateDate{
    if(!_labelCreateDate){
        _labelCreateDate = [[UILabel alloc]init];
        _labelCreateDate.font = [UIFont systemFontOfSize:12.f];
        _labelCreateDate.textAlignment = NSTextAlignmentRight;
    }
    return _labelCreateDate;
}

-(UILabel *)labelLine{
    if(!_labelLine){
        _labelLine = [[UILabel alloc]init];
        
    }
    return _labelLine;
}
-(UILabel *)labelUserName{
    if(!_labelUserName){
        _labelUserName = [[UILabel alloc]init];
        
        _labelUserName.textAlignment = NSTextAlignmentLeft;
        _labelUserName.font = [UIFont systemFontOfSize:14.f];
    }
    return _labelUserName;
}
-(UILabel *)labelPayType{
    if(!_labelPayType){
        _labelPayType = [[UILabel alloc]init];
        
        _labelPayType.textAlignment = NSTextAlignmentCenter;
        _labelPayType.font = [UIFont systemFontOfSize:14.f];
        _labelPayType.layer.masksToBounds = YES;
        _labelPayType.layer.cornerRadius = 5.f;
        
        _labelPayType.layer.borderWidth = 1.f;
    }
    return _labelPayType;
}
- (UIButton *)zhqsBtn
{
    if (!_zhqsBtn) {
        _zhqsBtn = [[UIButton alloc]init];
        _zhqsBtn.backgroundColor = [UIColor clearColor];
        _zhqsBtn.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [_zhqsBtn setTitle:@"召唤骑士" forState:UIControlStateNormal];
        _zhqsBtn.layer.cornerRadius = 5;
        _zhqsBtn.layer.masksToBounds = YES;
    }
    return _zhqsBtn;
}
@end
